/*
 * Created: 10.03.2008
 * File: Todo4M
 */

package org.urchr.todo4m;

import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.WaitScreen;
import org.netbeans.microedition.util.SimpleCancellableTask;
import org.urchr.todoist.lib.DashboardTopic;
import org.urchr.todoist.lib.Order;
import org.urchr.todoist.lib.Project;
import org.urchr.todoist.lib.Task;
import org.urchr.todoist.lib.Todoist;

/**
 * @author Kostya
 */
public class Todo4M extends MIDlet implements CommandListener {

    
    private String queryString = "";
    private boolean midletPaused = false;
    private Vector projects = new Vector();
    private Vector tasks = new Vector();
    private Vector dTasks = new Vector();
    private boolean showDashboardAfterLoad = false;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private java.util.Hashtable __previousDisplayables = new java.util.Hashtable();
    private List projectList;
    private WaitScreen waitScreen;
    private Form addTaskForm;
    private TextField taskDate;
    private TextField taskName;
    private ChoiceGroup priority;
    private List taskList;
    private Alert noItem;
    private Alert alert;
    private Alert cantComplete;
    private Alert cantDelete;
    private Alert ordersCount;
    private Alert cantAdd;
    private Alert cantCommit;
    private Alert reloadQuestion;
    private WaitScreen commitScreen;
    private Alert tokenNotSet;
    private List dashboardList;
    private Alert cantLoadD;
    private WaitScreen loadDashboard1;
    private Command loadData;
    private Command exitCommand;
    private Command okCommand;
    private Command addTask;
    private Command cancelCommand;
    private Command showTask;
    private Command goBack;
    private Command selectItem;
    private Command completeItem;
    private Command deleteTask;
    private Command showStatus;
    private Command collapseExpand;
    private Command commitOrders;
    private Command backCommand;
    private Command exitCommand1;
    private Command okCommand1;
    private Command cancelCommand1;
    private Command collapseProject;
    private Command showDashboard;
    private Command collapseExpandD;
    private Command completeTaskD;
    private Command backCommand1;
    private Command loadDashboard;
    private SimpleCancellableTask loadTask;
    private SimpleCancellableTask commitTask;
    private SimpleCancellableTask dashboardTask;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The Todo4M constructor.
     */
    public Todo4M() {
    }

    private void addSubProjects(Project project) {
        if(project.isCollapsed())
            return;
        for (int i = 0; i < project.getSubProjectCount(); i++) {
            Project p = project.getSubProjectAt(i);
            projectList.append(p.toString(), null);
            projects.addElement(p);
            addSubProjects(p);
        }

    }

    private boolean checkAddTaskParams() {
        if("".equals(taskName.getString()))
            return false;
        return true;
    }

    private boolean checkCanCompleteTask() {
        if(taskList.getSelectedIndex()==-1)
            return false;
        Task task = (Task) tasks.elementAt(taskList.getSelectedIndex());
        return task.isCheckable() && !task.isChecked();
    }

    private boolean checkCanDeleteTask() {
        if(taskList.getSelectedIndex()==-1)
            return false;
        return true;
    }


    private boolean checkProjectListItem() {
        if(projectList.getSelectedIndex()==-1)
            return false;
        return true;
    }

    private void addSubTasks(Task task){
        taskList.append(task.toString(), null);
        tasks.addElement(task);
        if(task.isCollapsed())
            return;
        for (int i = 0; i < task.getSubTasksCount(); i++) {
            Task t = task.getSubTaskAt(i);
            addSubTasks(t);
        }

    }

    private boolean checkTokenIsSet() {
        queryString = getAppProperty("dashboard-query");
        if(queryString==null || "".equals(queryString))
            queryString = "od, tod, tom";
        if("true".equals(getAppProperty("start-with-dashboard")))
            showDashboardAfterLoad = true;
        System.out.println("Token is "+getAppProperty("todoist-token"));
        if("not-set".equals(getAppProperty("todoist-token")) || getAppProperty("todoist-token")==null){
//            Todoist.getInstance().setToken("Insert your token here for debug");
//            return true;
            return false;
        }else{
            Todoist.getInstance().setToken(getAppProperty("todoist-token"));
            return true;
        }
        
    }

    private void doCollapseExpand() {
        int selected = taskList.getSelectedIndex();
        if(selected!=-1){
            Task t = (Task) tasks.elementAt(selected);
            if(t.getSubTasksCount()==0)
                return;
            t.setCollapsed(!t.isCollapsed());
            doReloadTasks(t.getProject());
            taskList.setSelectedIndex(selected, true);            
        }
    }

    private void doCollapseProject() {
        if(projectList.getSelectedIndex()==-1)
            return;
        Project project = (Project) projects.elementAt(projectList.getSelectedIndex());
        if(project.getSubProjectCount()>0){
            project.setCollapsed(!project.isCollapsed());
            int sel = projectList.getSelectedIndex();
            reloadProjects();
            projectList.setSelectedIndex(sel, true);
        }
    }

    private void doCollapseTopic() {
        int selected = dashboardList.getSelectedIndex();
        if(selected==-1)
            return;
        if(!(dTasks.elementAt(selected) instanceof DashboardTopic)){
            return;
        }
        DashboardTopic topic = (DashboardTopic) dTasks.elementAt(selected);
        topic.setCollapsed(!topic.isCollapsed());
        reloadDashboard();
        dashboardList.setSelectedIndex(selected, true);
    }

    private void doCompleteTask() {
        Task task = (Task) tasks.elementAt(taskList.getSelectedIndex());
        task.setChecked(true);
        Todoist.getInstance().addOrder(new Order(Order.COMPLETE_TASK, task));
        
        task.setOrderAction(Order.COMPLETE_TASK);
        Todoist.getInstance().ChangeTask(task);

        int selected = taskList.getSelectedIndex();
        doReloadTasks(task.getProject());
        reloadDashboard();
        taskList.setSelectedIndex(selected, true);
    }

    private void doCompleteTaskD() {
        int selected = dashboardList.getSelectedIndex();
        if(selected==-1)
            return;
        if(!(dTasks.elementAt(selected) instanceof Task)){
            return;
        }
        Task task = (Task) dTasks.elementAt(selected);
        task.setChecked(true);
        Todoist.getInstance().addOrder(new Order(Order.COMPLETE_TASK, task));

        task.setOrderAction(Order.COMPLETE_TASK);
        Todoist.getInstance().ChangeTask(task);
        
        reloadDashboard();
        dashboardList.setSelectedIndex(selected, true);        
    }

    private void doDeleteTask() {
        Task task = (Task) tasks.elementAt(taskList.getSelectedIndex());
        Todoist.getInstance().addOrder(new Order(Order.DELETE_TASK, task));

        task.setOrderAction(Order.DELETE_TASK);
        Todoist.getInstance().ChangeTask(task);

        for (int i = 0; i < task.getSubTasksCount(); i++) {
            Task t = task.getSubTaskAt(i);
            if(task.getParentTask()!=null)
                task.getParentTask().addSubTask(t);
            else
                task.getProject().addTask(t);
            
        }

        if(task.getParentTask()!=null)
            task.getParentTask().removeSubTask(task);
        else
            task.getProject().removeTask(task);
        doReloadTasks(task.getProject());
        //!Taks deleting
    }
    
    private void doReloadTasks(Project project) {
        taskList.deleteAll();
        tasks.removeAllElements();
        for (int i = 0; i < project.getTasksCount(); i++) {
            Task t = project.getTaskAt(i);
            addSubTasks(t);
        }

    }

    private void doShowDashboard() {
//        Todoist.getInstance().makeQuery("\"tod\", tom");
    }
    
    private void reloadDashboard(){
        dashboardList.deleteAll();
        dTasks.removeAllElements();
        for (int i = 0; i < Todoist.getInstance().getTopicsCount(); i++) {
            DashboardTopic topic = Todoist.getInstance().getTopicAt(i);
            dashboardList.append("->"+(topic.isCollapsed()?"...":"")+topic.getName(), null);
            dTasks.addElement(topic);
            if(topic.isCollapsed())
                continue;
            for (int j = 0; j < topic.getTaskCount(); j++) {
                Task task = topic.getTaskAt(j);
                dashboardList.append(task.toDashboardString(), null);
                dTasks.addElement(task);
            }
        }
    }
    
    private void reloadProjects(){
        projectList.deleteAll();
        projects.removeAllElements();
        for (int i = 0; i < Todoist.getInstance().getProjectsCount(); i++) {
            Project p = Todoist.getInstance().getProjectAt(i);
            projectList.append(p.toString(), null);
            projects.addElement(p);
            addSubProjects(p);
        }
        if (projectList.size() > 0) projectList.setSelectedIndex(0, true);
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    /**
     * Switches a display to previous displayable of the current displayable.
     * The <code>display</code> instance is obtain from the <code>getDisplay</code> method.
     */
    private void switchToPreviousDisplayable()
    {
        Displayable __currentDisplayable = getDisplay().getCurrent();
        if (__currentDisplayable != null)
        {
            Displayable __nextDisplayable = (Displayable) __previousDisplayables.get(__currentDisplayable);
            if (__nextDisplayable != null)
            {
                switchDisplayable(null, __nextDisplayable);
            }
        }
    }
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize()
    {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
        projectList = new List("Please select project:", Choice.IMPLICIT);//GEN-BEGIN:|0-initialize|1|0-postInitialize
        projectList.addCommand(getLoadData());
        projectList.addCommand(getExitCommand());
        projectList.addCommand(getSelectItem());
        projectList.addCommand(getShowStatus());
        projectList.addCommand(getCommitOrders());
        projectList.addCommand(getCollapseProject());
        projectList.addCommand(getShowDashboard());
        projectList.setCommandListener(this);
        projectList.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
        projectList.setSelectCommand(getSelectItem());
        projectList.setSelectedFlags(new boolean[] {  });
        taskList = new List("Please select task:", Choice.IMPLICIT);
        taskList.addCommand(getShowTask());
        taskList.addCommand(getGoBack());
        taskList.addCommand(getCompleteItem());
        taskList.addCommand(getDeleteTask());
        taskList.addCommand(getCollapseExpand());
        taskList.addCommand(getAddTask());
        taskList.setCommandListener(this);
        taskList.setFitPolicy(Choice.TEXT_WRAP_ON);
        taskList.setSelectCommand(getShowTask());
        ordersCount = new Alert("Status:", "Changes: "+Todoist.getInstance().getOrdersCount(), null, AlertType.INFO);
        ordersCount.setTimeout(Alert.FOREVER);
        dashboardList = new List("Dashboard", Choice.IMPLICIT);
        dashboardList.addCommand(getBackCommand1());
        dashboardList.addCommand(getCompleteTaskD());
        dashboardList.addCommand(getCollapseExpandD());
        dashboardList.addCommand(getCommitOrders());
        dashboardList.addCommand(getLoadData());
        dashboardList.addCommand(getLoadDashboard());
        dashboardList.setCommandListener(this);
        dashboardList.setFitPolicy(Choice.TEXT_WRAP_ON);
        dashboardList.setSelectCommand(getCompleteTaskD());//GEN-END:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
        Todoist.getInstance().LoadProjects();
        reloadProjects();
        reloadDashboard();
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet()
    {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here

        checkToken();//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet()
    {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable)
    {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        Displayable __currentDisplayable = display.getCurrent();
        if (__currentDisplayable != null  &&  nextDisplayable != null)
        {
            __previousDisplayables.put(nextDisplayable, __currentDisplayable);
        }
        if (alert == null)
        {
            display.setCurrent(nextDisplayable);
        }
        else
        {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable)
    {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == addTaskForm)//GEN-BEGIN:|7-commandAction|1|46-preAction
        {
            if (command == cancelCommand)
            {//GEN-END:|7-commandAction|1|46-preAction
                // write pre-action user code here
                switchToPreviousDisplayable();//GEN-LINE:|7-commandAction|2|46-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|44-preAction
            else if (command == okCommand)
            {//GEN-END:|7-commandAction|3|44-preAction
                // write pre-action user code here
                checkAddTask();//GEN-LINE:|7-commandAction|4|44-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|136-preAction
        }
        else if (displayable == commitScreen)
        {
            if (command == WaitScreen.FAILURE_COMMAND)
            {//GEN-END:|7-commandAction|5|136-preAction
                // write pre-action user code here
                switchDisplayable(getCantCommit(), projectList);//GEN-LINE:|7-commandAction|6|136-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|135-preAction
            else if (command == WaitScreen.SUCCESS_COMMAND)
            {//GEN-END:|7-commandAction|7|135-preAction
                // write pre-action user code here
                switchDisplayable(getReloadQuestion(), getCommitScreen());//GEN-LINE:|7-commandAction|8|135-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|9|177-preAction
        }
        else if (displayable == dashboardList)
        {
            if (command == List.SELECT_COMMAND)
            {//GEN-END:|7-commandAction|9|177-preAction
                // write pre-action user code here
                dashboardListAction();//GEN-LINE:|7-commandAction|10|177-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|182-preAction
            else if (command == backCommand1)
            {//GEN-END:|7-commandAction|11|182-preAction
                // write pre-action user code here
                switchDisplayable(null, projectList);//GEN-LINE:|7-commandAction|12|182-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|13|188-preAction
            else if (command == collapseExpandD)
            {//GEN-END:|7-commandAction|13|188-preAction
                // write pre-action user code here
                doCollapseTopic();//GEN-LINE:|7-commandAction|14|188-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|204-preAction
            else if (command == commitOrders)
            {//GEN-END:|7-commandAction|15|204-preAction
                // write pre-action user code here
                switchDisplayable(null, getCommitScreen());//GEN-LINE:|7-commandAction|16|204-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|17|185-preAction
            else if (command == completeTaskD)
            {//GEN-END:|7-commandAction|17|185-preAction
                // write pre-action user code here
                doCompleteTaskD();//GEN-LINE:|7-commandAction|18|185-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|19|214-preAction
            else if (command == loadDashboard)
            {//GEN-END:|7-commandAction|19|214-preAction
                // write pre-action user code here
                switchDisplayable(null, getLoadDashboard1());//GEN-LINE:|7-commandAction|20|214-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|207-preAction
            else if (command == loadData)
            {//GEN-END:|7-commandAction|21|207-preAction
                // write pre-action user code here
                switchDisplayable(null, getWaitScreen());//GEN-LINE:|7-commandAction|22|207-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|23|211-preAction
        }
        else if (displayable == loadDashboard1)
        {
            if (command == WaitScreen.FAILURE_COMMAND)
            {//GEN-END:|7-commandAction|23|211-preAction
                // write pre-action user code here
                switchDisplayable(getCantLoadD(), dashboardList);//GEN-LINE:|7-commandAction|24|211-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|25|210-preAction
            else if (command == WaitScreen.SUCCESS_COMMAND)
            {//GEN-END:|7-commandAction|25|210-preAction
                // write pre-action user code here
                switchDisplayable(null, dashboardList);//GEN-LINE:|7-commandAction|26|210-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|27|15-preAction
        }
        else if (displayable == projectList)
        {
            if (command == List.SELECT_COMMAND)
            {//GEN-END:|7-commandAction|27|15-preAction
                // write pre-action user code here
                projectListAction();//GEN-LINE:|7-commandAction|28|15-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|29|169-preAction
            else if (command == collapseProject)
            {//GEN-END:|7-commandAction|29|169-preAction
                // write pre-action user code here
                doCollapseProject();//GEN-LINE:|7-commandAction|30|169-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|31|133-preAction
            else if (command == commitOrders)
            {//GEN-END:|7-commandAction|31|133-preAction
                // write pre-action user code here
                switchDisplayable(null, getCommitScreen());//GEN-LINE:|7-commandAction|32|133-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|33|31-preAction
            else if (command == exitCommand)
            {//GEN-END:|7-commandAction|33|31-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|34|31-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|35|26-preAction
            else if (command == loadData)
            {//GEN-END:|7-commandAction|35|26-preAction
                // write pre-action user code here
                switchDisplayable(null, getWaitScreen());//GEN-LINE:|7-commandAction|36|26-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|37|67-preAction
            else if (command == selectItem)
            {//GEN-END:|7-commandAction|37|67-preAction
                // write pre-action user code here
                checkProjectItem1();//GEN-LINE:|7-commandAction|38|67-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|39|173-preAction
            else if (command == showDashboard)
            {//GEN-END:|7-commandAction|39|173-preAction
                // write pre-action user code here
                switchDisplayable(null, dashboardList);//GEN-LINE:|7-commandAction|40|173-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|41|103-preAction
            else if (command == showStatus)
            {//GEN-END:|7-commandAction|41|103-preAction
                // write pre-action user code here
                ordersCount.setString("Changes: "+Todoist.getInstance().getOrdersCount());
                switchDisplayable(ordersCount, projectList);//GEN-LINE:|7-commandAction|42|103-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|43|147-preAction
        }
        else if (displayable == reloadQuestion)
        {
            if (command == cancelCommand1)
            {//GEN-END:|7-commandAction|43|147-preAction
                // write pre-action user code here
                checkShowDashboard();//GEN-LINE:|7-commandAction|44|147-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|45|145-preAction
            else if (command == okCommand1)
            {//GEN-END:|7-commandAction|45|145-preAction
                // write pre-action user code here
                checkShowDashboard1();//GEN-LINE:|7-commandAction|46|145-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|47|64-preAction
        }
        else if (displayable == taskList)
        {
            if (command == List.SELECT_COMMAND)
            {//GEN-END:|7-commandAction|47|64-preAction
                // write pre-action user code here
                taskListAction();//GEN-LINE:|7-commandAction|48|64-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|49|130-preAction
            else if (command == addTask)
            {//GEN-END:|7-commandAction|49|130-preAction
                // write pre-action user code here
                getTaskName().setString("");
                getTaskDate().setString("");
                getPriority().setSelectedIndex(4, true);
                getAddTaskForm().setTitle("Add new task:");
                switchDisplayable(null, getAddTaskForm());//GEN-LINE:|7-commandAction|50|130-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|51|115-preAction
            else if (command == collapseExpand)
            {//GEN-END:|7-commandAction|51|115-preAction
                // write pre-action user code here
                doCollapseExpand();//GEN-LINE:|7-commandAction|52|115-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|53|75-preAction
            else if (command == completeItem)
            {//GEN-END:|7-commandAction|53|75-preAction
                // write pre-action user code here
                checkCanComplete();//GEN-LINE:|7-commandAction|54|75-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|55|92-preAction
            else if (command == deleteTask)
            {//GEN-END:|7-commandAction|55|92-preAction
                // write pre-action user code here
                checkDeleteTask();//GEN-LINE:|7-commandAction|56|92-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|57|72-preAction
            else if (command == goBack)
            {//GEN-END:|7-commandAction|57|72-preAction
                // write pre-action user code here
                switchDisplayable(null, projectList);//GEN-LINE:|7-commandAction|58|72-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|59|70-preAction
            else if (command == showTask)
            {//GEN-END:|7-commandAction|59|70-preAction
                // write pre-action user code here
                if(taskList.getSelectedIndex()==-1)
                    return;
                Task task = (Task) tasks.elementAt(taskList.getSelectedIndex());
                System.out.println("Loading task name: "+task.getName());
                getTaskName().setString(task.getName());
                getTaskDate().setString(task.getDate());
                int selectedPriority = task.getPriority()-1;
                if(selectedPriority<0)
                    selectedPriority = 4;
                if(selectedPriority>4)
                    selectedPriority = 4;
                System.out.println("Setting priority to "+selectedPriority+", task has "+task.getPriority());
                getPriority().setSelectedIndex(selectedPriority, true);
                getAddTaskForm().setTitle("Edit task:");
                switchDisplayable(null, getAddTaskForm());//GEN-LINE:|7-commandAction|60|70-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|61|160-preAction
        }
        else if (displayable == tokenNotSet)
        {
            if (command == exitCommand1)
            {//GEN-END:|7-commandAction|61|160-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|62|160-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|63|23-preAction
        }
        else if (displayable == waitScreen)
        {
            if (command == WaitScreen.FAILURE_COMMAND)
            {//GEN-END:|7-commandAction|63|23-preAction
                // write pre-action user code here
                switchDisplayable(getAlert(), projectList);//GEN-LINE:|7-commandAction|64|23-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|65|22-preAction
            else if (command == WaitScreen.SUCCESS_COMMAND)
            {//GEN-END:|7-commandAction|65|22-preAction
                // write pre-action user code here
                checkShowDashboard();//GEN-LINE:|7-commandAction|66|22-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|67|7-postCommandAction
        }//GEN-END:|7-commandAction|67|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|68|
    //</editor-fold>//GEN-END:|7-commandAction|68|







    //<editor-fold defaultstate="collapsed" desc=" Generated Method: projectListAction ">//GEN-BEGIN:|13-action|0|13-preAction
    /**
     * Performs an action assigned to the selected list element in the projectList component.
     */
    public void projectListAction()
    {//GEN-END:|13-action|0|13-preAction
        // enter pre-action user code here
        String __selectedString = projectList.getString(projectList.getSelectedIndex());//GEN-LINE:|13-action|1|13-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|13-action|2|
    //</editor-fold>//GEN-END:|13-action|2|


    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: waitScreen ">//GEN-BEGIN:|19-getter|0|19-preInit
    /**
     * Returns an initiliazed instance of waitScreen component.
     * @return the initialized component instance
     */
    public WaitScreen getWaitScreen()
    {
        if (waitScreen == null)
        {//GEN-END:|19-getter|0|19-preInit
            // write pre-init user code here
            waitScreen = new WaitScreen(getDisplay());//GEN-BEGIN:|19-getter|1|19-postInit
            waitScreen.setTitle("Please wait...");
            waitScreen.setCommandListener(this);
            waitScreen.setText("Loading data from server");
            waitScreen.setTask(getLoadTask());//GEN-END:|19-getter|1|19-postInit
            // write post-init user code here
        }//GEN-BEGIN:|19-getter|2|
        return waitScreen;
    }
    //</editor-fold>//GEN-END:|19-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loadData ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of loadData component.
     * @return the initialized component instance
     */
    public Command getLoadData()
    {
        if (loadData == null)
        {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            loadData = new Command("Load tasks", Command.SCREEN, 0);//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return loadData;
    }
    //</editor-fold>//GEN-END:|25-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loadTask ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of loadTask component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getLoadTask()
    {
        if (loadTask == null)
        {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            loadTask = new SimpleCancellableTask();//GEN-BEGIN:|24-getter|1|24-execute
            loadTask.setExecutable(new org.netbeans.microedition.util.Executable()
            {
                public void execute() throws Exception
                {//GEN-END:|24-getter|1|24-execute
                    projectList.deleteAll();
                    projects.removeAllElements();
                    dashboardList.deleteAll();
                    dTasks.removeAllElements();
                    if(!Todoist.getInstance().doRefreshData())
                        throw new Exception("Can't load data from server");
                    if(!Todoist.getInstance().makeQuery(queryString))
                        throw new Exception("Can't load data from server 2");
                    reloadProjects();
                    reloadDashboard();
                }//GEN-BEGIN:|24-getter|2|24-postInit
            });//GEN-END:|24-getter|2|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|3|
        return loadTask;
    }
    //</editor-fold>//GEN-END:|24-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand()
    {
        if (exitCommand == null)
        {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: addTaskForm ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of addTaskForm component.
     * @return the initialized component instance
     */
    public Form getAddTaskForm()
    {
        if (addTaskForm == null)
        {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            addTaskForm = new Form("Add new task:", new Item[] { getTaskName(), getTaskDate(), getPriority() });//GEN-BEGIN:|33-getter|1|33-postInit
            addTaskForm.addCommand(getOkCommand());
            addTaskForm.addCommand(getCancelCommand());
            addTaskForm.setCommandListener(this);//GEN-END:|33-getter|1|33-postInit
            // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return addTaskForm;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: taskName ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of taskName component.
     * @return the initialized component instance
     */
    public TextField getTaskName()
    {
        if (taskName == null)
        {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            taskName = new TextField("Enter task contents:", "", 2048, TextField.ANY);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return taskName;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: taskDate ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of taskDate component.
     * @return the initialized component instance
     */
    public TextField getTaskDate()
    {
        if (taskDate == null)
        {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            taskDate = new TextField("Enter date:", "", 32, TextField.ANY);//GEN-LINE:|38-getter|1|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return taskDate;
    }
    //</editor-fold>//GEN-END:|38-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Method: doAddTask ">//GEN-BEGIN:|50-entry|0|51-preAction
    /**
     * Performs an action assigned to the doAddTask entry-point.
     */
    public void doAddTask()
    {//GEN-END:|50-entry|0|51-preAction
        // write pre-action user code here
        boolean isEdit = getAddTaskForm().getTitle().equals("Edit task:");
        Project project = (Project) projects.elementAt(projectList.getSelectedIndex());
        Task task = null;
        task = new Task(project, null);
        if(isEdit)
            task = (Task) tasks.elementAt(taskList.getSelectedIndex());
        task.setName(taskName.getString());
        task.setDate(taskDate.getString());
        task.setPriority(""+(priority.getSelectedIndex()+1));
        if(!isEdit){
            project.addTask(task);
            Todoist.getInstance().addOrder(new Order(Order.ADD_TASK, task));

            task.setOrderAction(Order.ADD_TASK);
            Todoist.getInstance().SaveTask(task);
        }else{
            Todoist.getInstance().addOrder(new Order(Order.EDIT_TASK, task));

            task.setOrderAction(Order.EDIT_TASK);
            Todoist.getInstance().ChangeTask(task);
        }
        doReloadTasks(project);
        switchDisplayable(null, taskList);//GEN-LINE:|50-entry|1|51-postAction
        // write post-action user code here
    }//GEN-BEGIN:|50-entry|2|
    //</editor-fold>//GEN-END:|50-entry|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: addTask ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of addTask component.
     * @return the initialized component instance
     */
    public Command getAddTask()
    {
        if (addTask == null)
        {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            addTask = new Command("Add task", Command.ITEM, 0);//GEN-LINE:|40-getter|1|40-postInit
            // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return addTask;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand()
    {
        if (okCommand == null)
        {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand ">//GEN-BEGIN:|45-getter|0|45-preInit
    /**
     * Returns an initiliazed instance of cancelCommand component.
     * @return the initialized component instance
     */
    public Command getCancelCommand()
    {
        if (cancelCommand == null)
        {//GEN-END:|45-getter|0|45-preInit
            // write pre-init user code here
            cancelCommand = new Command("Cancel", Command.BACK, 0);//GEN-LINE:|45-getter|1|45-postInit
            // write post-init user code here
        }//GEN-BEGIN:|45-getter|2|
        return cancelCommand;
    }
    //</editor-fold>//GEN-END:|45-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of alert component.
     * @return the initialized component instance
     */
    public Alert getAlert()
    {
        if (alert == null)
        {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            alert = new Alert("Error:", "Can\'t load data from server", null, AlertType.ERROR);//GEN-BEGIN:|61-getter|1|61-postInit
            alert.setTimeout(Alert.FOREVER);//GEN-END:|61-getter|1|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|2|
        return alert;
    }
    //</editor-fold>//GEN-END:|61-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: noItem ">//GEN-BEGIN:|62-getter|0|62-preInit
    /**
     * Returns an initiliazed instance of noItem component.
     * @return the initialized component instance
     */
    public Alert getNoItem()
    {
        if (noItem == null)
        {//GEN-END:|62-getter|0|62-preInit
            // write pre-init user code here
            noItem = new Alert("Error:", "Project is not selected", null, AlertType.ERROR);//GEN-BEGIN:|62-getter|1|62-postInit
            noItem.setTimeout(Alert.FOREVER);//GEN-END:|62-getter|1|62-postInit
            // write post-init user code here
        }//GEN-BEGIN:|62-getter|2|
        return noItem;
    }
    //</editor-fold>//GEN-END:|62-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: taskListAction ">//GEN-BEGIN:|63-action|0|63-preAction
    /**
     * Performs an action assigned to the selected list element in the taskList component.
     */
    public void taskListAction()
    {//GEN-END:|63-action|0|63-preAction
        // enter pre-action user code here
        String __selectedString = taskList.getString(taskList.getSelectedIndex());//GEN-LINE:|63-action|1|63-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|63-action|2|
    //</editor-fold>//GEN-END:|63-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: selectItem ">//GEN-BEGIN:|66-getter|0|66-preInit
    /**
     * Returns an initiliazed instance of selectItem component.
     * @return the initialized component instance
     */
    public Command getSelectItem()
    {
        if (selectItem == null)
        {//GEN-END:|66-getter|0|66-preInit
            // write pre-init user code here
            selectItem = new Command("Show tasks", Command.ITEM, 0);//GEN-LINE:|66-getter|1|66-postInit
            // write post-init user code here
        }//GEN-BEGIN:|66-getter|2|
        return selectItem;
    }
    //</editor-fold>//GEN-END:|66-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: showTask ">//GEN-BEGIN:|69-getter|0|69-preInit
    /**
     * Returns an initiliazed instance of showTask component.
     * @return the initialized component instance
     */
    public Command getShowTask()
    {
        if (showTask == null)
        {//GEN-END:|69-getter|0|69-preInit
            // write pre-init user code here
            showTask = new Command("Edit task", Command.OK, 0);//GEN-LINE:|69-getter|1|69-postInit
            // write post-init user code here
        }//GEN-BEGIN:|69-getter|2|
        return showTask;
    }
    //</editor-fold>//GEN-END:|69-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: goBack ">//GEN-BEGIN:|71-getter|0|71-preInit
    /**
     * Returns an initiliazed instance of goBack component.
     * @return the initialized component instance
     */
    public Command getGoBack()
    {
        if (goBack == null)
        {//GEN-END:|71-getter|0|71-preInit
            // write pre-init user code here
            goBack = new Command("Cancel", Command.CANCEL, 0);//GEN-LINE:|71-getter|1|71-postInit
            // write post-init user code here
        }//GEN-BEGIN:|71-getter|2|
        return goBack;
    }
    //</editor-fold>//GEN-END:|71-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: completeItem ">//GEN-BEGIN:|74-getter|0|74-preInit
    /**
     * Returns an initiliazed instance of completeItem component.
     * @return the initialized component instance
     */
    public Command getCompleteItem()
    {
        if (completeItem == null)
        {//GEN-END:|74-getter|0|74-preInit
            // write pre-init user code here
            completeItem = new Command("Complete task", Command.ITEM, 0);//GEN-LINE:|74-getter|1|74-postInit
            // write post-init user code here
        }//GEN-BEGIN:|74-getter|2|
        return completeItem;
    }
    //</editor-fold>//GEN-END:|74-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: checkProjectItem1 ">//GEN-BEGIN:|80-if|0|80-preIf
    /**
     * Performs an action assigned to the checkProjectItem1 if-point.
     */
    public void checkProjectItem1()
    {//GEN-END:|80-if|0|80-preIf
        // enter pre-if user code here
        if (checkProjectListItem())//GEN-BEGIN:|80-if|1|81-preAction
        {//GEN-END:|80-if|1|81-preAction
            // write pre-action user code here
            reloadTasks();//GEN-LINE:|80-if|2|81-postAction
            // write post-action user code here
        }//GEN-BEGIN:|80-if|3|82-preAction
        else
        {//GEN-END:|80-if|3|82-preAction
            // write pre-action user code here
            switchDisplayable(null, getNoItem());//GEN-LINE:|80-if|4|82-postAction
            // write post-action user code here
        }//GEN-LINE:|80-if|5|80-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|80-if|6|
    //</editor-fold>//GEN-END:|80-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: reloadTasks ">//GEN-BEGIN:|87-entry|0|88-preAction
    /**
     * Performs an action assigned to the reloadTasks entry-point.
     */
    public void reloadTasks()
    {//GEN-END:|87-entry|0|88-preAction
        // write pre-action user code here
        doReloadTasks((Project)projects.elementAt(projectList.getSelectedIndex()));
        switchDisplayable(null, taskList);//GEN-LINE:|87-entry|1|88-postAction
        // write post-action user code here
    }//GEN-BEGIN:|87-entry|2|
    //</editor-fold>//GEN-END:|87-entry|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: checkCanComplete ">//GEN-BEGIN:|95-if|0|95-preIf
    /**
     * Performs an action assigned to the checkCanComplete if-point.
     */
    public void checkCanComplete()
    {//GEN-END:|95-if|0|95-preIf
        // enter pre-if user code here
        if (checkCanCompleteTask())//GEN-BEGIN:|95-if|1|96-preAction
        {//GEN-END:|95-if|1|96-preAction
            // write pre-action user code here
            doCompleteTask();//GEN-LINE:|95-if|2|96-postAction
                    // write post-action user code here
        }//GEN-BEGIN:|95-if|3|97-preAction
        else
        {//GEN-END:|95-if|3|97-preAction
            // write pre-action user code here
            switchDisplayable(null, getCantComplete());//GEN-LINE:|95-if|4|97-postAction
            // write post-action user code here
        }//GEN-LINE:|95-if|5|95-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|95-if|6|
    //</editor-fold>//GEN-END:|95-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cantComplete ">//GEN-BEGIN:|100-getter|0|100-preInit
    /**
     * Returns an initiliazed instance of cantComplete component.
     * @return the initialized component instance
     */
    public Alert getCantComplete()
    {
        if (cantComplete == null)
        {//GEN-END:|100-getter|0|100-preInit
            // write pre-init user code here
            cantComplete = new Alert("Error:", "Can\'t complete task", null, AlertType.ERROR);//GEN-BEGIN:|100-getter|1|100-postInit
            cantComplete.setTimeout(Alert.FOREVER);//GEN-END:|100-getter|1|100-postInit
            // write post-init user code here
        }//GEN-BEGIN:|100-getter|2|
        return cantComplete;
    }
    //</editor-fold>//GEN-END:|100-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: deleteTask ">//GEN-BEGIN:|91-getter|0|91-preInit
    /**
     * Returns an initiliazed instance of deleteTask component.
     * @return the initialized component instance
     */
    public Command getDeleteTask()
    {
        if (deleteTask == null)
        {//GEN-END:|91-getter|0|91-preInit
            // write pre-init user code here
            deleteTask = new Command("Delete task", Command.ITEM, 0);//GEN-LINE:|91-getter|1|91-postInit
            // write post-init user code here
        }//GEN-BEGIN:|91-getter|2|
        return deleteTask;
    }
    //</editor-fold>//GEN-END:|91-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: showStatus ">//GEN-BEGIN:|102-getter|0|102-preInit
    /**
     * Returns an initiliazed instance of showStatus component.
     * @return the initialized component instance
     */
    public Command getShowStatus()
    {
        if (showStatus == null)
        {//GEN-END:|102-getter|0|102-preInit
            // write pre-init user code here
            showStatus = new Command("Show status", Command.ITEM, 0);//GEN-LINE:|102-getter|1|102-postInit
            // write post-init user code here
        }//GEN-BEGIN:|102-getter|2|
        return showStatus;
    }
    //</editor-fold>//GEN-END:|102-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: checkDeleteTask ">//GEN-BEGIN:|106-if|0|106-preIf
    /**
     * Performs an action assigned to the checkDeleteTask if-point.
     */
    public void checkDeleteTask()
    {//GEN-END:|106-if|0|106-preIf
        // enter pre-if user code here
        if (checkCanDeleteTask())//GEN-BEGIN:|106-if|1|107-preAction
        {//GEN-END:|106-if|1|107-preAction
            // write pre-action user code here
            doDeleteTask();//GEN-LINE:|106-if|2|107-postAction
            // write post-action user code here
        }//GEN-BEGIN:|106-if|3|108-preAction
        else
        {//GEN-END:|106-if|3|108-preAction
            // write pre-action user code here
            switchDisplayable(null, getCantDelete());//GEN-LINE:|106-if|4|108-postAction
            // write post-action user code here
        }//GEN-LINE:|106-if|5|106-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|106-if|6|
    //</editor-fold>//GEN-END:|106-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cantDelete ">//GEN-BEGIN:|110-getter|0|110-preInit
    /**
     * Returns an initiliazed instance of cantDelete component.
     * @return the initialized component instance
     */
    public Alert getCantDelete()
    {
        if (cantDelete == null)
        {//GEN-END:|110-getter|0|110-preInit
            // write pre-init user code here
            cantDelete = new Alert("Error:", "Can\'t delete task", null, AlertType.ERROR);//GEN-BEGIN:|110-getter|1|110-postInit
            cantDelete.setTimeout(Alert.FOREVER);//GEN-END:|110-getter|1|110-postInit
            // write post-init user code here
        }//GEN-BEGIN:|110-getter|2|
        return cantDelete;
    }
    //</editor-fold>//GEN-END:|110-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: collapseExpand ">//GEN-BEGIN:|114-getter|0|114-preInit
    /**
     * Returns an initiliazed instance of collapseExpand component.
     * @return the initialized component instance
     */
    public Command getCollapseExpand()
    {
        if (collapseExpand == null)
        {//GEN-END:|114-getter|0|114-preInit
            // write pre-init user code here
            collapseExpand = new Command("Collapse/Expand task", Command.ITEM, 0);//GEN-LINE:|114-getter|1|114-postInit
            // write post-init user code here
        }//GEN-BEGIN:|114-getter|2|
        return collapseExpand;
    }
    //</editor-fold>//GEN-END:|114-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: priority ">//GEN-BEGIN:|118-getter|0|118-preInit
    /**
     * Returns an initiliazed instance of priority component.
     * @return the initialized component instance
     */
    public ChoiceGroup getPriority()
    {
        if (priority == null)
        {//GEN-END:|118-getter|0|118-preInit
            // write pre-init user code here
            priority = new ChoiceGroup("Choose priority:", Choice.POPUP);//GEN-BEGIN:|118-getter|1|118-postInit
            priority.append("priority 1", null);
            priority.append("priority 2", null);
            priority.append("priority 3", null);
            priority.append("priority 4", null);
            priority.append("Not set", null);
            priority.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            priority.setSelectedFlags(new boolean[] { false, false, false, false, true });//GEN-END:|118-getter|1|118-postInit
            // write post-init user code here
        }//GEN-BEGIN:|118-getter|2|
        return priority;
    }
    //</editor-fold>//GEN-END:|118-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: checkAddTask ">//GEN-BEGIN:|123-if|0|123-preIf
    /**
     * Performs an action assigned to the checkAddTask if-point.
     */
    public void checkAddTask()
    {//GEN-END:|123-if|0|123-preIf
        // enter pre-if user code here
        if (checkAddTaskParams())//GEN-BEGIN:|123-if|1|124-preAction
        {//GEN-END:|123-if|1|124-preAction
            // write pre-action user code here
            doAddTask();//GEN-LINE:|123-if|2|124-postAction
            // write post-action user code here
        }//GEN-BEGIN:|123-if|3|125-preAction
        else
        {//GEN-END:|123-if|3|125-preAction
            // write pre-action user code here
            switchDisplayable(null, getCantAdd());//GEN-LINE:|123-if|4|125-postAction
            // write post-action user code here
        }//GEN-LINE:|123-if|5|123-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|123-if|6|
    //</editor-fold>//GEN-END:|123-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cantAdd ">//GEN-BEGIN:|127-getter|0|127-preInit
    /**
     * Returns an initiliazed instance of cantAdd component.
     * @return the initialized component instance
     */
    public Alert getCantAdd()
    {
        if (cantAdd == null)
        {//GEN-END:|127-getter|0|127-preInit
            // write pre-init user code here
            cantAdd = new Alert("Error:", "Can\'t add task. Invalid parameters", null, AlertType.ERROR);//GEN-BEGIN:|127-getter|1|127-postInit
            cantAdd.setTimeout(Alert.FOREVER);//GEN-END:|127-getter|1|127-postInit
            // write post-init user code here
        }//GEN-BEGIN:|127-getter|2|
        return cantAdd;
    }
    //</editor-fold>//GEN-END:|127-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: commitScreen ">//GEN-BEGIN:|134-getter|0|134-preInit
    /**
     * Returns an initiliazed instance of commitScreen component.
     * @return the initialized component instance
     */
    public WaitScreen getCommitScreen()
    {
        if (commitScreen == null)
        {//GEN-END:|134-getter|0|134-preInit
            // write pre-init user code here
            commitScreen = new WaitScreen(getDisplay());//GEN-BEGIN:|134-getter|1|134-postInit
            commitScreen.setTitle("Please wait...");
            commitScreen.setCommandListener(this);
            commitScreen.setText("Commiting data");
            commitScreen.setTask(getCommitTask());//GEN-END:|134-getter|1|134-postInit
            // write post-init user code here
        }//GEN-BEGIN:|134-getter|2|
        return commitScreen;
    }
    //</editor-fold>//GEN-END:|134-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cantCommit ">//GEN-BEGIN:|140-getter|0|140-preInit
    /**
     * Returns an initiliazed instance of cantCommit component.
     * @return the initialized component instance
     */
    public Alert getCantCommit()
    {
        if (cantCommit == null)
        {//GEN-END:|140-getter|0|140-preInit
            // write pre-init user code here
            cantCommit = new Alert("Error:", "Can\'t commit data", null, AlertType.ERROR);//GEN-BEGIN:|140-getter|1|140-postInit
            cantCommit.setTimeout(Alert.FOREVER);//GEN-END:|140-getter|1|140-postInit
            // write post-init user code here
        }//GEN-BEGIN:|140-getter|2|
        return cantCommit;
    }
    //</editor-fold>//GEN-END:|140-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: commitOrders ">//GEN-BEGIN:|132-getter|0|132-preInit
    /**
     * Returns an initiliazed instance of commitOrders component.
     * @return the initialized component instance
     */
    public Command getCommitOrders()
    {
        if (commitOrders == null)
        {//GEN-END:|132-getter|0|132-preInit
            // write pre-init user code here
            commitOrders = new Command("Commit", Command.SCREEN, 0);//GEN-LINE:|132-getter|1|132-postInit
            // write post-init user code here
        }//GEN-BEGIN:|132-getter|2|
        return commitOrders;
    }
    //</editor-fold>//GEN-END:|132-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: commitTask ">//GEN-BEGIN:|137-getter|0|137-preInit
    /**
     * Returns an initiliazed instance of commitTask component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getCommitTask()
    {
        if (commitTask == null)
        {//GEN-END:|137-getter|0|137-preInit
            // write pre-init user code here
            commitTask = new SimpleCancellableTask();//GEN-BEGIN:|137-getter|1|137-execute
            commitTask.setExecutable(new org.netbeans.microedition.util.Executable()
            {
                public void execute() throws Exception
                {//GEN-END:|137-getter|1|137-execute
                    if(!Todoist.getInstance().commitOrders())
                        throw new Exception("Can't commit");
                }//GEN-BEGIN:|137-getter|2|137-postInit
            });//GEN-END:|137-getter|2|137-postInit
            // write post-init user code here
        }//GEN-BEGIN:|137-getter|3|
        return commitTask;
    }
    //</editor-fold>//GEN-END:|137-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: reloadQuestion ">//GEN-BEGIN:|143-getter|0|143-preInit
    /**
     * Returns an initiliazed instance of reloadQuestion component.
     * @return the initialized component instance
     */
    public Alert getReloadQuestion()
    {
        if (reloadQuestion == null)
        {//GEN-END:|143-getter|0|143-preInit
            // write pre-init user code here
            reloadQuestion = new Alert("Question", "Load data from server?", null, AlertType.CONFIRMATION);//GEN-BEGIN:|143-getter|1|143-postInit
            reloadQuestion.addCommand(getOkCommand1());
            reloadQuestion.addCommand(getCancelCommand1());
            reloadQuestion.setCommandListener(this);
            reloadQuestion.setTimeout(Alert.FOREVER);//GEN-END:|143-getter|1|143-postInit
            // write post-init user code here
        }//GEN-BEGIN:|143-getter|2|
        return reloadQuestion;
    }
    //</editor-fold>//GEN-END:|143-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|144-getter|0|144-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1()
    {
        if (okCommand1 == null)
        {//GEN-END:|144-getter|0|144-preInit
            // write pre-init user code here
            okCommand1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|144-getter|1|144-postInit
            // write post-init user code here
        }//GEN-BEGIN:|144-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|144-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cancelCommand1 ">//GEN-BEGIN:|146-getter|0|146-preInit
    /**
     * Returns an initiliazed instance of cancelCommand1 component.
     * @return the initialized component instance
     */
    public Command getCancelCommand1()
    {
        if (cancelCommand1 == null)
        {//GEN-END:|146-getter|0|146-preInit
            // write pre-init user code here
            cancelCommand1 = new Command("Cancel", Command.CANCEL, 0);//GEN-LINE:|146-getter|1|146-postInit
            // write post-init user code here
        }//GEN-BEGIN:|146-getter|2|
        return cancelCommand1;
    }
    //</editor-fold>//GEN-END:|146-getter|2|







    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|153-getter|0|153-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand()
    {
        if (backCommand == null)
        {//GEN-END:|153-getter|0|153-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.CANCEL, 0);//GEN-LINE:|153-getter|1|153-postInit
            // write post-init user code here
        }//GEN-BEGIN:|153-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|153-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tokenNotSet ">//GEN-BEGIN:|158-getter|0|158-preInit
    /**
     * Returns an initiliazed instance of tokenNotSet component.
     * @return the initialized component instance
     */
    public Alert getTokenNotSet()
    {
        if (tokenNotSet == null)
        {//GEN-END:|158-getter|0|158-preInit
            // write pre-init user code here
            tokenNotSet = new Alert("Fatal error!", "Token is not set, please enter your Todoist.com token into program JAD", null, AlertType.ERROR);//GEN-BEGIN:|158-getter|1|158-postInit
            tokenNotSet.addCommand(getExitCommand1());
            tokenNotSet.setCommandListener(this);
            tokenNotSet.setTimeout(Alert.FOREVER);//GEN-END:|158-getter|1|158-postInit
            // write post-init user code here
        }//GEN-BEGIN:|158-getter|2|
        return tokenNotSet;
    }
    //</editor-fold>//GEN-END:|158-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|159-getter|0|159-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1()
    {
        if (exitCommand1 == null)
        {//GEN-END:|159-getter|0|159-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|159-getter|1|159-postInit
            // write post-init user code here
        }//GEN-BEGIN:|159-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|159-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: checkToken ">//GEN-BEGIN:|162-if|0|162-preIf
    /**
     * Performs an action assigned to the checkToken if-point.
     */
    public void checkToken()
    {//GEN-END:|162-if|0|162-preIf
        // enter pre-if user code here
        if (checkTokenIsSet())//GEN-BEGIN:|162-if|1|163-preAction
        {//GEN-END:|162-if|1|163-preAction
            // write pre-action user code here
            switchDisplayable(null, projectList);//GEN-LINE:|162-if|2|163-postAction
            // write post-action user code here
        }//GEN-BEGIN:|162-if|3|164-preAction
        else
        {//GEN-END:|162-if|3|164-preAction
            // write pre-action user code here
            switchDisplayable(null, getTokenNotSet());//GEN-LINE:|162-if|4|164-postAction
            // write post-action user code here
        }//GEN-LINE:|162-if|5|162-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|162-if|6|
    //</editor-fold>//GEN-END:|162-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: collapseProject ">//GEN-BEGIN:|168-getter|0|168-preInit
    /**
     * Returns an initiliazed instance of collapseProject component.
     * @return the initialized component instance
     */
    public Command getCollapseProject()
    {
        if (collapseProject == null)
        {//GEN-END:|168-getter|0|168-preInit
            // write pre-init user code here
            collapseProject = new Command("Collapse/Expand", Command.SCREEN, 0);//GEN-LINE:|168-getter|1|168-postInit
            // write post-init user code here
        }//GEN-BEGIN:|168-getter|2|
        return collapseProject;
    }
    //</editor-fold>//GEN-END:|168-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: showDashboard ">//GEN-BEGIN:|172-getter|0|172-preInit
    /**
     * Returns an initiliazed instance of showDashboard component.
     * @return the initialized component instance
     */
    public Command getShowDashboard()
    {
        if (showDashboard == null)
        {//GEN-END:|172-getter|0|172-preInit
            // write pre-init user code here
            showDashboard = new Command("Dashboard", Command.SCREEN, 0);//GEN-LINE:|172-getter|1|172-postInit
            // write post-init user code here
        }//GEN-BEGIN:|172-getter|2|
        return showDashboard;
    }
    //</editor-fold>//GEN-END:|172-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Method: dashboardListAction ">//GEN-BEGIN:|176-action|0|176-preAction
    /**
     * Performs an action assigned to the selected list element in the dashboardList component.
     */
    public void dashboardListAction()
    {//GEN-END:|176-action|0|176-preAction
        // enter pre-action user code here
        String __selectedString = dashboardList.getString(dashboardList.getSelectedIndex());//GEN-LINE:|176-action|1|176-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|176-action|2|
    //</editor-fold>//GEN-END:|176-action|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|181-getter|0|181-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1()
    {
        if (backCommand1 == null)
        {//GEN-END:|181-getter|0|181-preInit
            // write pre-init user code here
            backCommand1 = new Command("Project list", Command.BACK, 0);//GEN-LINE:|181-getter|1|181-postInit
            // write post-init user code here
        }//GEN-BEGIN:|181-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|181-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: completeTaskD ">//GEN-BEGIN:|184-getter|0|184-preInit
    /**
     * Returns an initiliazed instance of completeTaskD component.
     * @return the initialized component instance
     */
    public Command getCompleteTaskD()
    {
        if (completeTaskD == null)
        {//GEN-END:|184-getter|0|184-preInit
            // write pre-init user code here
            completeTaskD = new Command("Complete task", Command.ITEM, 0);//GEN-LINE:|184-getter|1|184-postInit
            // write post-init user code here
        }//GEN-BEGIN:|184-getter|2|
        return completeTaskD;
    }
    //</editor-fold>//GEN-END:|184-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: collapseExpandD ">//GEN-BEGIN:|187-getter|0|187-preInit
    /**
     * Returns an initiliazed instance of collapseExpandD component.
     * @return the initialized component instance
     */
    public Command getCollapseExpandD()
    {
        if (collapseExpandD == null)
        {//GEN-END:|187-getter|0|187-preInit
            // write pre-init user code here
            collapseExpandD = new Command("Collapse/Expand", Command.ITEM, 0);//GEN-LINE:|187-getter|1|187-postInit
            // write post-init user code here
        }//GEN-BEGIN:|187-getter|2|
        return collapseExpandD;
    }
    //</editor-fold>//GEN-END:|187-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: checkShowDashboard ">//GEN-BEGIN:|192-if|0|192-preIf
    /**
     * Performs an action assigned to the checkShowDashboard if-point.
     */
    public void checkShowDashboard()
    {//GEN-END:|192-if|0|192-preIf
        // enter pre-if user code here
        if (showDashboardAfterLoad)//GEN-BEGIN:|192-if|1|193-preAction
        {//GEN-END:|192-if|1|193-preAction
            // write pre-action user code here
            switchDisplayable(null, dashboardList);//GEN-LINE:|192-if|2|193-postAction
            // write post-action user code here
        }//GEN-BEGIN:|192-if|3|194-preAction
        else
        {//GEN-END:|192-if|3|194-preAction
            // write pre-action user code here
            switchDisplayable(null, projectList);//GEN-LINE:|192-if|4|194-postAction
            // write post-action user code here
        }//GEN-LINE:|192-if|5|192-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|192-if|6|
    //</editor-fold>//GEN-END:|192-if|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loadDashboard1 ">//GEN-BEGIN:|209-getter|0|209-preInit
    /**
     * Returns an initiliazed instance of loadDashboard1 component.
     * @return the initialized component instance
     */
    public WaitScreen getLoadDashboard1()
    {
        if (loadDashboard1 == null)
        {//GEN-END:|209-getter|0|209-preInit
            // write pre-init user code here
            loadDashboard1 = new WaitScreen(getDisplay());//GEN-BEGIN:|209-getter|1|209-postInit
            loadDashboard1.setTitle("Please wait...");
            loadDashboard1.setCommandListener(this);
            loadDashboard1.setText("Loading dashboard...");
            loadDashboard1.setTask(getDashboardTask());//GEN-END:|209-getter|1|209-postInit
            // write post-init user code here
        }//GEN-BEGIN:|209-getter|2|
        return loadDashboard1;
    }
    //</editor-fold>//GEN-END:|209-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cantLoadD ">//GEN-BEGIN:|217-getter|0|217-preInit
    /**
     * Returns an initiliazed instance of cantLoadD component.
     * @return the initialized component instance
     */
    public Alert getCantLoadD()
    {
        if (cantLoadD == null)
        {//GEN-END:|217-getter|0|217-preInit
            // write pre-init user code here
            cantLoadD = new Alert("Error:", "Can\'t reload dashboard", null, AlertType.ERROR);//GEN-BEGIN:|217-getter|1|217-postInit
            cantLoadD.setTimeout(Alert.FOREVER);//GEN-END:|217-getter|1|217-postInit
            // write post-init user code here
        }//GEN-BEGIN:|217-getter|2|
        return cantLoadD;
    }
    //</editor-fold>//GEN-END:|217-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: loadDashboard ">//GEN-BEGIN:|213-getter|0|213-preInit
    /**
     * Returns an initiliazed instance of loadDashboard component.
     * @return the initialized component instance
     */
    public Command getLoadDashboard()
    {
        if (loadDashboard == null)
        {//GEN-END:|213-getter|0|213-preInit
            // write pre-init user code here
            loadDashboard = new Command("Load dashboard", Command.SCREEN, 0);//GEN-LINE:|213-getter|1|213-postInit
            // write post-init user code here
        }//GEN-BEGIN:|213-getter|2|
        return loadDashboard;
    }
    //</editor-fold>//GEN-END:|213-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dashboardTask ">//GEN-BEGIN:|212-getter|0|212-preInit
    /**
     * Returns an initiliazed instance of dashboardTask component.
     * @return the initialized component instance
     */
    public SimpleCancellableTask getDashboardTask()
    {
        if (dashboardTask == null)
        {//GEN-END:|212-getter|0|212-preInit
            // write pre-init user code here
            dashboardTask = new SimpleCancellableTask();//GEN-BEGIN:|212-getter|1|212-execute
            dashboardTask.setExecutable(new org.netbeans.microedition.util.Executable()
            {
                public void execute() throws Exception
                {//GEN-END:|212-getter|1|212-execute
                    dashboardList.deleteAll();
                    dTasks.removeAllElements();
                    if(!Todoist.getInstance().makeQuery(queryString))
                        throw new Exception("Can't load dashboard");
                    reloadDashboard();

                }//GEN-BEGIN:|212-getter|2|212-postInit
            });//GEN-END:|212-getter|2|212-postInit
            // write post-init user code here
        }//GEN-BEGIN:|212-getter|3|
        return dashboardTask;
    }
    //</editor-fold>//GEN-END:|212-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: checkShowDashboard1 ">//GEN-BEGIN:|219-if|0|219-preIf
    /**
     * Performs an action assigned to the checkShowDashboard1 if-point.
     */
    public void checkShowDashboard1()
    {//GEN-END:|219-if|0|219-preIf
        // enter pre-if user code here
        if (true)//GEN-BEGIN:|219-if|1|220-preAction
        {//GEN-END:|219-if|1|220-preAction
            // write pre-action user code here
            switchDisplayable(null, getLoadDashboard1());//GEN-LINE:|219-if|2|220-postAction
            // write post-action user code here
        }//GEN-BEGIN:|219-if|3|221-preAction
        else
        {//GEN-END:|219-if|3|221-preAction
            // write pre-action user code here
            switchDisplayable(null, getWaitScreen());//GEN-LINE:|219-if|4|221-postAction
            // write post-action user code here
        }//GEN-LINE:|219-if|5|219-postIf
        // enter post-if user code here
    }//GEN-BEGIN:|219-if|6|
    //</editor-fold>//GEN-END:|219-if|6|



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
